package com.example.java.arithmetic;

import java.util.Arrays;

public class ArithmeticApp {

    private static int binarySearch(int[] arr, int target){
        if (arr.length == 0){
            return -1;
        }
        int low = 0;
        int high = arr.length-1;
        return binarySearch(arr, target, low, high);
    }

    private static int binarySearch(int[] arr, int target, int low, int high){
        if (low > high){
            return -1;
        }
        int mid = (low + high)/2;
        if (arr[mid] == target){
            return mid;
        }else if (arr[mid] < target){
            return binarySearch(arr, target, mid + 1, high);
        }else {
            return binarySearch(arr, target, low, mid -1);
        }
    }

    private static void quickSortPointer(int[] arr, int low, int high){
        if (low >= high)return;
        int pointer = high;
        int pivot = arr[high];
//        int high = arr.length-1;
//        int pivot = arr[pointer];
//        int low = 0;
        for (int i = low; i< high;i++){
            if (arr[i] > arr[pointer]){
                int temp = arr[i];
                arr[i] = arr[pointer];
                arr[pointer] = temp;
                pointer = i;
            }
        }

        quickSortPointer(arr, low, pointer-1);
        quickSortPointer(arr, pointer+1, high);
//        return pointer;
    }

    private static void quickSort(int[] arr){
        quickSortPointer(arr, 0, arr.length-1);
    }

    private static void testQuickSort(){
        int[] a = {2,7,8,6,4,4,8,4,5,6,2,7,5,2,8,3,6,1,6};
        quickSort(a);
        System.out.println(Arrays.toString(a));
    }

    private static void testBinarySearch(){
        int[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
        int index = binarySearch(a, 8);
        System.out.println(index);
    }

}
